package fhq.hcmute.edu.vn.activities;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import fhq.hcmute.edu.vn.camera.CameraControls;
import fhq.hcmute.edu.vn.camera.CameraPreview;
import fhq.hcmute.edu.vn.languages.Language;

public class BarcodeScannerActivity extends Activity {
	private CameraPreview mPreview;
	private CameraControls cameraControls;
	private Button btnCancel;

	private void init() {
		cameraControls = new CameraControls(this);
		try {
			cameraControls.open();
			mPreview = cameraControls
					.showCameraPreview(BarcodeScannerActivity.this);
			LinearLayout preview =
					(LinearLayout) findViewById(R.id.cameraPreview);
			preview.addView(mPreview);
		} catch (Exception e) {
			e.printStackTrace();
		}
		btnCancel = (Button) findViewById(R.id.btnCancelScan);
		btnCancel.setOnClickListener(onCancelClickListener);
	}

	private OnClickListener onCancelClickListener = new View.OnClickListener() {

		@Override
		public void onClick(final View v) {
			cameraControls.releaseCamera();
			finish();
		}
	};

	@Override
	protected final void onCreate(final Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Language.updateLanguageSystem(getBaseContext());
		setContentView(R.layout.activity_barcode_scanner);
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		init();
	}

	public final void onPause() {
		cameraControls.releaseCamera();
		super.onPause();
	}
}
